import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const v = new Vue();
export default new Vuex.Store({
  state: {
    tabList: [{
      name: 'HomeWelcomePage',
      label: "首页",
      component: "HomeWelcomePage"
    }],//打开的tab页
    activeTab: "HomeWelcomePage",//选中的tab页
  },
  mutations: {
    //关闭tab页
    closeTab(state, n) {
      let tab = state.tabList;
      tab.forEach((v, i) => {
        if (v.name == n) {
          tab.splice(i, 1);
          this.commit('judgeClose', i);
          n.split('-').length > 1 && this.commit('removeSession', n);
        }
      })
    },
    judgeClose(state, n) {
      let tab = state.tabList;
      let active = state.activeTab;
      let flag = true;
      for (const i of tab) {
        if (i.name == active) {
          flag = false;
          break;
        }
      }
      if (flag) {
        n - 1 > -1 ? state.activeTab = tab[n - 1].name : state.activeTab = tab[n].name
      }
    },
    removeSession(state, n) {
      let a = n.split('-');
      let s = JSON.parse(sessionStorage.getItem(a[0]));
      for (const i in s) {
        if (s[i].name == n) {
          s.splice(i, 1);
          break;
        }
      }
      s = sessionStorage.setItem(a[0], JSON.stringify(s));
    },
    //新增tab页
    addTab(state, n) {
      let tab = state.tabList;
      n['refresh'] || (n['refresh'] = true);
      // n.hasOwnProperty('refresh') || (n['refresh'] = true);
      let flag = true
      tab.find(v => v.name == n.name) || (tab.push(n), flag = false)
      state.activeTab = n.name;
      flag && (v.$common.refreshTab(n.name))
    },
    updateActiveTab(state, n) {
      state.activeTab = n;
    }
  },
  actions: {
  },
  modules: {
  }
})
